﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace TPT.RSControl
{
    public partial class UILog : UserControl
    {
        private static int _maxLogmsgTextLength = 10000;//日志框最大输入
        public UILog()
        {
            InitializeComponent();
        }

        private void UILog_Load(object sender, EventArgs e)
        {
            this.txtLogMsg.Multiline = true;
            this.txtLogMsg.ReadOnly = true; 
        }

        public static int richTextNum = 0;
        public void AppendLogMsg(string msg, Color color)
        {
            try
            {
                msg = DateTime.Now.ToString("HH:mm:ss.ffff") + "：" + msg;
                //在UI线程中执行
                txtLogMsg.BeginInvoke(new Action(() =>
                {
                    if (txtLogMsg.Lines.Count() > 50)
                    {
                        txtLogMsg.Clear();
                        //richTextNum = 0;
                    }
                        

                    txtLogMsg.Text = txtLogMsg.Text.Insert(0, Environment.NewLine);
                    txtLogMsg.Text = txtLogMsg.Text.Insert(0, msg);

                    //txtLogMsg.AppendText(msg);
                    //int selectedText = txtLogMsg.Text.IndexOf(msg);
                    //txtLogMsg.AppendText(Environment.NewLine);
                    //richTextNum++;
                    ////dfsdf
                    //if (selectedText == -1) selectedText = 0;
                    txtLogMsg.Select(0, (msg).Length);
                    txtLogMsg.SelectionColor = color;
                    
                }));
            }
            catch (Exception exc)
            {
            }
        }

        private void UILog_ClientSizeChanged(object sender, EventArgs e)
        {
            txtLogMsg.Width = this.Width;
            txtLogMsg.Height = this.Height;
        }
    }
}
